﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.Base;
using BBMS.DLL.DataContract;
using ExcelLibrary.SpreadSheet;

namespace BBMS.DLL.Forms.Bill.Collector
{
    public partial class frmCollectorFilter : FrmFilterBase
    {
        public frmCollectorFilter()
        {
            InitializeComponent();
            dtpTuNgayCT.Value = new DateTime(2000, 01, 01);
            GetMaNCCList();
        }

        private void GetMaNCCList()
        {
            ArrayList arret = NhaCungCapList.getAllMaNCC();
            cbxMaNCC.Items.Add(new KeyValuePair("", "Tất cả"));
            if (arret != null)
            {
                for (int i = 0; i < arret.Count; i++)
                {
                    KeyValuePair kvp = (KeyValuePair)arret[i];
                    cbxMaNCC.Items.Add(kvp);
                }
            }
            cbxMaNCC.SelectedIndex = 0;
        }

        public new static readonly string FunctionKey = "CT_CTTH";
        protected override string GetFunctionKey() { return "CT_CTTH"; }
        protected override BBMS.DLL.Base.ObjectBase GetObjectBase() { return new ChungTuThuHo(); }

        protected override void GenColHeaders()
        {
            lvList.Columns.Clear();
            lvList.Columns.Add("Ký hiệu", 80);
            lvList.Columns[0].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Số hiệu", 80);
            lvList.Columns[1].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Ngày CT", 80);
            lvList.Columns[2].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Nhà cung cấp", 200);
            lvList.Columns.Add("Công ty thu hộ", 150);
            lvList.Columns.Add("Giá trị", 100);
            lvList.Columns[5].TextAlign = HorizontalAlignment.Right;
        }

        protected override ArrayList GenSampleData()
        {
            ArrayList ar = new ArrayList();
            ar.Add(new string[] { "STT", "Mã chứng từ", " Số chứng từ", "Ngày chứng từ"
                , "Ngày thanh toán dự kiến", "Ngày thanh toán"
            ,"Trạng thái thanh toán", "Mã hồ sơ", "Mã nhà cung cấp", "Mã công ty thu hộ"
            , "Giá trị chứng từ" ,"Giá trị đã thanh toán", "Ghi chú", "Mã nhân viên"});
            ChungTuThuHo ChungTuThuHoList = new ChungTuThuHo();
            ArrayList arr = ChungTuThuHoList.GetAllObject(fParams);
            if (arr != null)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    ChungTuThuHo ctth = (ChungTuThuHo)arr[i];
                    ar.Add(new string[] { ctth.ID.ToString(), ctth.MaCT,ctth.SoCT,
                        ctth.NgayCT.ToString(), ctth.NgayTTDuKien.ToString()
                    ,ctth.NgayTT.ToString(),((int)ctth.TrangThaiTT).ToString(),ctth.MaHoSo,ctth.MaNCC
                    ,ctth.MaQuay,ctth.GiaTriCT.ToString(),ctth.GiaTriDaTT.ToString(), ctth.GhiChu, ctth.NVID.ToString()});
                }
            }
            return ar;
        }

        protected override ArrayList GetImportedList(Worksheet sheet)
        {
            ArrayList arret = new ArrayList();
            for (int i = 1; i < sheet.Cells.Rows.Count; i++)
            {
                ChungTuThuHo ctth = new ChungTuThuHo();
                Cell cell = sheet.Cells[i, 0];
                ctth.ID = Convert.ToInt32(cell.Value.ToString());
                cell = sheet.Cells[i, 1];
                ctth.MaCT = cell.Value.ToString();
                cell = sheet.Cells[i, 2];
                ctth.SoCT = cell.Value.ToString();
                cell = sheet.Cells[i, 3];
                if (cell.Value.ToString() != "") ctth.NgayCT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 4];                
                if (cell.Value.ToString() != "") ctth.NgayTTDuKien = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 5];
                if (cell.Value.ToString() != "") ctth.NgayTT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 6];
                foreach (TrangThaiThanhToan item in Enum.GetValues(typeof(TrangThaiThanhToan)))
                {
                    if (item.ToString() == cell.Value.ToString())
                        ctth.TrangThaiTT = item;
                }
                cell = sheet.Cells[i, 7];
                ctth.MaHoSo = cell.Value.ToString();
                cell = sheet.Cells[i, 8];
                ctth.MaNCC = cell.Value.ToString();
                cell = sheet.Cells[i, 9];
                ctth.MaCTyThuHo = cell.Value.ToString();
                cell = sheet.Cells[i, 10];
                if (cell.Value.ToString() != "") ctth.GiaTriCT = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 11];
                if (cell.Value.ToString() != "") ctth.GiaTriDaTT = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 12];
                ctth.GhiChu = cell.Value.ToString();
                cell = sheet.Cells[i, 13];
                if (cell.Value.ToString() != "") ctth.NVID = Convert.ToInt32(cell.Value.ToString());

                arret.Add(ctth);

            }
            return arret;
        }
        
        protected override void UpdateFilterParams()
        {
            if (txtMa.Text.Trim() != "" || txtSoCT.Text.Trim() != "" || cbxMaNCC.SelectedIndex!=-1)
            {
                fParams = new FilterParams();
                //FilterCondition fcLoaiCTTH = new FilterCondition("LoaiCT", "LoaiCT", "CTTH", SQLOperator.EQUAL);
                //fParams.ArParams.Add(fcLoaiCTTH);
                //FilterCondition fcIsChi = new FilterCondition("IsChi", "IsChi", false, SQLOperator.EQUAL);
                //fParams.ArParams.Add(fcIsChi);
                if (txtMa.Text.Trim() != "")
                {
                    FilterCondition fcMa = new FilterCondition("MaCT", "MaCT", "%" + txtMa.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcMa);
                }

                if (txtSoCT.Text.Trim() != "")
                {
                    FilterCondition fcSoCT = new FilterCondition("SoCT", "SoCT", "%" + txtSoCT.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcSoCT);
                }

                if (cbxMaNCC.SelectedIndex != 0)
                {
                    FilterCondition fcMaNCC = new FilterCondition("MaNCC", "MaNCC", ((KeyValuePair)cbxMaNCC.SelectedItem).Key, SQLOperator.EQUAL);
                    fParams.ArParams.Add(fcMaNCC);
                }

                FilterCondition fcTuNgayCT = new FilterCondition("NgayCT", "TuNgayCT", new DateTime(dtpTuNgayCT.Value.Year, dtpTuNgayCT.Value.Month, dtpTuNgayCT.Value.Day, 0, 0, 0), SQLOperator.GREATER_OR_EQUAL);
                fParams.ArParams.Add(fcTuNgayCT);

                FilterCondition fcDenNgayCT = new FilterCondition("NgayCT", "DenNgayCT", new DateTime(dtDenNgayCT.Value.Year, dtDenNgayCT.Value.Month, dtDenNgayCT.Value.Day, 23, 59, 59), SQLOperator.LOWER_OR_EQUAL);
                fParams.ArParams.Add(fcDenNgayCT);

               
                fParams.CurrentPage = 0;
            }
            else
            {
                MessageBox.Show(null, "Bạn phải nhập nội dung tìm kiếm", "Thông báo");
            }
        }

        protected override FrmEditBase CreateEditForm(BBMS.DLL.Base.ObjectBase obj)
        {
            return new frmCollectUpdate(obj);
        }

        protected override void AddNewObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            if (obj == null) return;
            ListViewItem it = new ListViewItem(((ChungTuThuHo)obj).MaCT);
            it.SubItems.Add(((ChungTuThuHo)obj).SoCT);
            it.SubItems.Add(((ChungTuThuHo)obj).NgayCT.ToString("dd/MM/yyyy"));
            it.SubItems.Add(ObjectLists.NhaCungCapList.getObjectName(((ChungTuThuHo)obj).MaNCC));
            it.SubItems.Add(ObjectLists.CTyThuHoList.getObjectName(((ChungTuThuHo)obj).MaCTyThuHo));
            it.SubItems.Add(CommonTools.Helper.MoneyString(((ChungTuThuHo)obj).GiaTriCT));
            it.Tag = obj;
            lvList.Items.Add(it);
        }

        protected override void UpdateObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            foreach (ListViewItem it in lvList.Items)
            {
                if (((ChungTuThuHo)it.Tag).MaCT == ((ChungTuThuHo)obj).MaCT)
                {
                    it.SubItems[2].Text = ((ChungTuThuHo)obj).NgayCT.ToString("dd/MM/yyyy");
                    it.SubItems[3].Text = ObjectLists.NhaCungCapList.getObjectName(((ChungTuThuHo)obj).MaNCC);
                    it.SubItems[4].Text = ObjectLists.CTyThuHoList.getObjectName(((ChungTuThuHo)obj).MaCTyThuHo);
                    it.SubItems[5].Text = CommonTools.Helper.MoneyString(((ChungTuThuHo)obj).GiaTriCT);
                    it.Tag = obj;
                    break;
                }
            }
        }

        //protected override void OnRefreshClick()
        //{
        //    fParams = new FilterParams();
        //    FilterCondition fcLoaiCTTH = new FilterCondition("LoaiCT", "LoaiCT", "CTTH", SQLOperator.EQUAL);
        //    fParams.ArParams.Add(fcLoaiCTTH);
        //    FilterCondition fcIsChi = new FilterCondition("IsChi", "IsChi", false, SQLOperator.EQUAL);
        //    fParams.ArParams.Add(fcIsChi);
        //    ListObject();
        //}       
    }
}
